<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>remotenamefetcher.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_i18n_uchar_remotenamefetcher.js.html">remotenamefetcher.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Object which fetches Unicode codepoint names from a remote data
<a name="line17"></a> * source. This data source should accept two parameters:
<a name="line18"></a> * &lt;ol&gt;
<a name="line19"></a> * &lt;li&gt;c - the list of codepoints in hexadecimal format
<a name="line20"></a> * &lt;li&gt;p - the name property
<a name="line21"></a> * &lt;/ol&gt;
<a name="line22"></a> * and return a JSON object representation of the result.
<a name="line23"></a> * For example, calling this data source with the following URL:
<a name="line24"></a> * http://datasource?c=50,ff,102bd&amp;p=name
<a name="line25"></a> * Should return a JSON object which looks like this:
<a name="line26"></a> * &lt;pre&gt;
<a name="line27"></a> * {&quot;50&quot;:{&quot;name&quot;:&quot;LATIN CAPITAL LETTER P&quot;},
<a name="line28"></a> * &quot;ff&quot;:{&quot;name&quot;:&quot;LATIN SMALL LETTER Y WITH DIAERESIS&quot;},
<a name="line29"></a> * &quot;102bd&quot;:{&quot;name&quot;:&quot;CARIAN LETTER K2&quot;}}
<a name="line30"></a> * &lt;/pre&gt;.
<a name="line31"></a> */
<a name="line32"></a>
<a name="line33"></a>goog.provide(&#39;goog.i18n.uChar.RemoteNameFetcher&#39;);
<a name="line34"></a>
<a name="line35"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line36"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line37"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line38"></a>goog.require(&#39;goog.i18n.uChar&#39;);
<a name="line39"></a>goog.require(&#39;goog.i18n.uChar.NameFetcher&#39;);
<a name="line40"></a>goog.require(&#39;goog.net.XhrIo&#39;);
<a name="line41"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Builds the RemoteNameFetcher object. This object retrieves codepoint names
<a name="line47"></a> * from a remote data source.
<a name="line48"></a> *
<a name="line49"></a> * @param {string} dataSourceUri URI to the data source.
<a name="line50"></a> * @constructor
<a name="line51"></a> * @implements {goog.i18n.uChar.NameFetcher}
<a name="line52"></a> * @extends {goog.Disposable}
<a name="line53"></a> */
<a name="line54"></a>goog.i18n.uChar.RemoteNameFetcher = function(dataSourceUri) {
<a name="line55"></a>  goog.base(this);
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * XHRIo object for prefetch() asynchronous calls.
<a name="line59"></a>   *
<a name="line60"></a>   * @type {!goog.net.XhrIo}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.prefetchXhrIo_ = new goog.net.XhrIo();
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * XHRIo object for getName() asynchronous calls.
<a name="line67"></a>   *
<a name="line68"></a>   * @type {!goog.net.XhrIo}
<a name="line69"></a>   * @private
<a name="line70"></a>   */
<a name="line71"></a>  this.getNameXhrIo_ = new goog.net.XhrIo();
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * URI to the data.
<a name="line75"></a>   *
<a name="line76"></a>   * @type {string}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.dataSourceUri_ = dataSourceUri;
<a name="line80"></a>
<a name="line81"></a>  /**
<a name="line82"></a>   * A cache of all the collected names from the server.
<a name="line83"></a>   *
<a name="line84"></a>   * @type {!goog.structs.Map}
<a name="line85"></a>   * @private
<a name="line86"></a>   */
<a name="line87"></a>  this.charNames_ = new goog.structs.Map();
<a name="line88"></a>};
<a name="line89"></a>goog.inherits(goog.i18n.uChar.RemoteNameFetcher, goog.Disposable);
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Key to the listener on XHR for prefetch(). Used to clear previous listeners.
<a name="line94"></a> *
<a name="line95"></a> * @type {goog.events.Key}
<a name="line96"></a> * @private
<a name="line97"></a> */
<a name="line98"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchLastListenerKey_;
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Key to the listener on XHR for getName(). Used to clear previous listeners.
<a name="line103"></a> *
<a name="line104"></a> * @type {goog.events.Key}
<a name="line105"></a> * @private
<a name="line106"></a> */
<a name="line107"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.getNameLastListenerKey_;
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * A reference to the RemoteNameFetcher logger.
<a name="line112"></a> *
<a name="line113"></a> * @type {!goog.debug.Logger}
<a name="line114"></a> * @private
<a name="line115"></a> */
<a name="line116"></a>goog.i18n.uChar.RemoteNameFetcher.logger_ =
<a name="line117"></a>    goog.debug.Logger.getLogger(&#39;goog.i18n.uChar.RemoteNameFetcher&#39;);
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/** @override */
<a name="line123"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.disposeInternal = function() {
<a name="line124"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line125"></a>  this.prefetchXhrIo_.dispose();
<a name="line126"></a>  this.getNameXhrIo_.dispose();
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/** @override */
<a name="line131"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.prefetch = function(characters) {
<a name="line132"></a>  // Abort the current request if there is one
<a name="line133"></a>  if (this.prefetchXhrIo_.isActive()) {
<a name="line134"></a>    goog.i18n.uChar.RemoteNameFetcher.logger_.
<a name="line135"></a>        info(&#39;Aborted previous prefetch() call for new incoming request&#39;);
<a name="line136"></a>    this.prefetchXhrIo_.abort();
<a name="line137"></a>  }
<a name="line138"></a>  if (this.prefetchLastListenerKey_) {
<a name="line139"></a>    goog.events.unlistenByKey(this.prefetchLastListenerKey_);
<a name="line140"></a>  }
<a name="line141"></a>
<a name="line142"></a>  // Set up new listener
<a name="line143"></a>  var preFetchCallback = goog.bind(this.prefetchCallback_, this);
<a name="line144"></a>  this.prefetchLastListenerKey_ = goog.events.listenOnce(this.prefetchXhrIo_,
<a name="line145"></a>      goog.net.EventType.COMPLETE, preFetchCallback);
<a name="line146"></a>
<a name="line147"></a>  this.fetch_(goog.i18n.uChar.RemoteNameFetcher.RequestType_.BASE_88,
<a name="line148"></a>      characters, this.prefetchXhrIo_);
<a name="line149"></a>};
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Callback on completion of the prefetch operation.
<a name="line154"></a> *
<a name="line155"></a> * @private
<a name="line156"></a> */
<a name="line157"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchCallback_ = function() {
<a name="line158"></a>  this.processResponse_(this.prefetchXhrIo_);
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/** @override */
<a name="line163"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.getName = function(character,
<a name="line164"></a>    callback) {
<a name="line165"></a>  var codepoint = goog.i18n.uChar.toCharCode(character).toString(16);
<a name="line166"></a>
<a name="line167"></a>  if (this.charNames_.containsKey(codepoint)) {
<a name="line168"></a>    var name = /** @type {string} */ (this.charNames_.get(codepoint));
<a name="line169"></a>    callback(name);
<a name="line170"></a>    return;
<a name="line171"></a>  }
<a name="line172"></a>
<a name="line173"></a>  // Abort the current request if there is one
<a name="line174"></a>  if (this.getNameXhrIo_.isActive()) {
<a name="line175"></a>    goog.i18n.uChar.RemoteNameFetcher.logger_.
<a name="line176"></a>        info(&#39;Aborted previous getName() call for new incoming request&#39;);
<a name="line177"></a>    this.getNameXhrIo_.abort();
<a name="line178"></a>  }
<a name="line179"></a>  if (this.getNameLastListenerKey_) {
<a name="line180"></a>    goog.events.unlistenByKey(this.getNameLastListenerKey_);
<a name="line181"></a>  }
<a name="line182"></a>
<a name="line183"></a>  // Set up new listener
<a name="line184"></a>  var getNameCallback = goog.bind(this.getNameCallback_, this, codepoint,
<a name="line185"></a>      callback);
<a name="line186"></a>  this.getNameLastListenerKey_ = goog.events.listenOnce(this.getNameXhrIo_,
<a name="line187"></a>      goog.net.EventType.COMPLETE, getNameCallback);
<a name="line188"></a>
<a name="line189"></a>  this.fetch_(goog.i18n.uChar.RemoteNameFetcher.RequestType_.CODEPOINT,
<a name="line190"></a>      codepoint, this.getNameXhrIo_);
<a name="line191"></a>};
<a name="line192"></a>
<a name="line193"></a>
<a name="line194"></a>/**
<a name="line195"></a> * Callback on completion of the getName operation.
<a name="line196"></a> *
<a name="line197"></a> * @param {string} codepoint The codepoint in hexadecimal format.
<a name="line198"></a> * @param {function(?string)} callback The callback function called when the
<a name="line199"></a> *     name retrieval is complete, contains a single string parameter with the
<a name="line200"></a> *     codepoint name, this parameter will be null if the character name is not
<a name="line201"></a> *     defined.
<a name="line202"></a> * @private
<a name="line203"></a> */
<a name="line204"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.getNameCallback_ = function(
<a name="line205"></a>    codepoint, callback) {
<a name="line206"></a>  this.processResponse_(this.getNameXhrIo_);
<a name="line207"></a>  var name = /** @type {?string} */ (this.charNames_.get(codepoint, null));
<a name="line208"></a>  callback(name);
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Process the response received from the server and store results in the cache.
<a name="line214"></a> *
<a name="line215"></a> * @param {!goog.net.XhrIo} xhrIo The XhrIo object used to make the request.
<a name="line216"></a> * @private
<a name="line217"></a> */
<a name="line218"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.processResponse_ = function(xhrIo) {
<a name="line219"></a>  if (!xhrIo.isSuccess()) {
<a name="line220"></a>    goog.i18n.uChar.RemoteNameFetcher.logger_.severe(
<a name="line221"></a>        &#39;Problem with data source: &#39; + xhrIo.getLastError());
<a name="line222"></a>    return;
<a name="line223"></a>  }
<a name="line224"></a>  var result = xhrIo.getResponseJson();
<a name="line225"></a>  for (var codepoint in result) {
<a name="line226"></a>    if (result[codepoint].hasOwnProperty(&#39;name&#39;)) {
<a name="line227"></a>      this.charNames_.set(codepoint, result[codepoint][&#39;name&#39;]);
<a name="line228"></a>    }
<a name="line229"></a>  }
<a name="line230"></a>};
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * Enum for the different request types.
<a name="line235"></a> *
<a name="line236"></a> * @enum {string}
<a name="line237"></a> * @private
<a name="line238"></a> */
<a name="line239"></a>goog.i18n.uChar.RemoteNameFetcher.RequestType_ = {
<a name="line240"></a>
<a name="line241"></a>  /**
<a name="line242"></a>   * Request type that uses a base 88 string containing a set of codepoints to
<a name="line243"></a>   * be fetched from the server (see goog.i18n.charpickerdata for more
<a name="line244"></a>   * information on b88).
<a name="line245"></a>   */
<a name="line246"></a>  BASE_88: &#39;b88&#39;,
<a name="line247"></a>
<a name="line248"></a>  /**
<a name="line249"></a>   * Request type that uses a a string of comma separated codepoint values.
<a name="line250"></a>   */
<a name="line251"></a>  CODEPOINT: &#39;c&#39;
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * Fetches a set of codepoint names from the data source.
<a name="line257"></a> *
<a name="line258"></a> * @param {!goog.i18n.uChar.RemoteNameFetcher.RequestType_} requestType The
<a name="line259"></a> *     request type of the operation. This parameter specifies how the server is
<a name="line260"></a> *     called to fetch a particular set of codepoints.
<a name="line261"></a> * @param {string} requestInput The input to the request, this is the value that
<a name="line262"></a> *     is passed onto the server to complete the request.
<a name="line263"></a> * @param {!goog.net.XhrIo} xhrIo The XHRIo object to execute the server call.
<a name="line264"></a> * @private
<a name="line265"></a> */
<a name="line266"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.fetch_ = function(requestType,
<a name="line267"></a>    requestInput, xhrIo) {
<a name="line268"></a>  var url = new goog.Uri(this.dataSourceUri_);
<a name="line269"></a>  url.setParameterValue(requestType, requestInput);
<a name="line270"></a>  url.setParameterValue(&#39;p&#39;, &#39;name&#39;);
<a name="line271"></a>  goog.i18n.uChar.RemoteNameFetcher.logger_.info(&#39;Request: &#39; +
<a name="line272"></a>      url.toString());
<a name="line273"></a>  xhrIo.send(url);
<a name="line274"></a>};
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/** @override */
<a name="line278"></a>goog.i18n.uChar.RemoteNameFetcher.prototype.isNameAvailable = function(
<a name="line279"></a>    character) {
<a name="line280"></a>  return true;
<a name="line281"></a>};
</pre>


</body>
</html>
